from itsdangerous import TimedJSONWebSignatureSerializer
#
#
# sk = 'abc'
# data={
#     'id':'1',
#     'email':'m18637702676@163.com'
# }
# # 2.创建序列化器
# serializer = TimedJSONWebSignatureSerializer(secret_key=sk,expires_in=3600)
# # 3.加密数据
# token = serializer.dumps(data)
# print(token)
#
#
#
# # 解密
#
# info=serializer.loads(token)
# print(info)
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from meiduo_shop import settings
def generate_active_email(data):
    # 1.加密秘钥，使用系统生成的，过期时间也放在setting
     # 2.创建序列化器
    s = Serializer(
         secret_key=settings.SECRET_KEY,
         expires_in = settings.EMAIL_EXPIRE)
    token =s.dumps(data).decode()
    # 3.加密生成token
    return 'http://www.meiduo.site:8080/success_verify_email.html?token='+token
    # bytes类型
    # 4.返回
def check_token(token):
    s = Serializer(
         secret_key=settings.SECRET_KEY,
         expires_in = settings.EMAIL_EXPIRE)
    # 数据过期或破坏，要异常捕获
    try:
        data = s.loads(token)
    except Exception as e:
        return None
    else:
        return data
